<template>
  <div class="berth-page">
    <breadcrumb />
    <div class="search-container">
      <Search @search="getSearchParams" />
    </div>
    <div class="table-container">
      <div class="table-component">
        <div class="submit-btns">
          <div></div>
          <div class="submit-btns-right">
            <el-button type="primary" class="submit-btn-add" @click="addData">
              <img class="icon-img" :src="iconAdd" alt="" />
              新增</el-button
            >
            <el-button
              type="primary"
              class="submit-btn-import"
              @click="importData"
            >
              <img class="icon-img" :src="iconImport" alt="" />导入</el-button
            >
          </div>
        </div>
        <div class="table">
          <el-table
            :data="table.data"
            :loading="table.loading"
            style="width: 100%"
            :header-cell-style="{
              background: '#F1F1F1',
              'font-size': '14px',
              'font-weight': 600,
              color: '#333333',
            }"
            :cell-style="{
              'font-size': '14px',
              'font-weight': 400,
              color: '#333333',
            }"
          >
            <el-table-column label="序号" min-width="60" fixed="left">
              <template #default="scope">
                {{ (scope.$index + 1).toString().padStart(2, "0") }}
              </template>
            </el-table-column>
            <el-table-column prop="code" label="泊位编号" min-width="100" />
            <el-table-column prop="section" label="路段名称" min-width="100" />
            <!-- <el-table-column prop="region" label="所属行政区" min-width="100" /> -->
            <el-table-column prop="type" label="泊位类型">
              <template #default="scope">
                {{ scope.row.type == 1 ? "普通泊位" : "特殊泊位" }}
              </template>
            </el-table-column>
            <el-table-column prop="status" label="使用状态">
              <template #default="scope">
                <div v-if="scope.row.status == 1" class="planning">规划中</div>
                <div v-else-if="scope.row.status == 2" class="NotProduction">
                  建设中
                </div>
                <div v-else-if="scope.row.status == 3" class="inUse">
                  使用中
                </div>
                <div v-else-if="scope.row.status == 4" class="underRepair">
                  维修中
                </div>
                <div v-else class="scrapped">已停用</div>
              </template>
            </el-table-column>
            <el-table-column prop="hasCar" label="泊位状态">
              <template #default="scope">
                {{ scope.row.hasCar === true ? "有车" : "无车" }}
              </template>
            </el-table-column>
            <el-table-column prop="stay" label="排序方式">
              <template #default="scope">
                {{ scope.row.stay === 1 ? "靠左" : "靠右" }}
              </template>
            </el-table-column>

            <el-table-column label="操作" width="110" fixed="right">
              <template #default="scope">
                <el-button
                  @click="editData(scope.row)"
                  type="primary"
                  size="small"
                  link
                  style="color: #007aff; font-size: 14px"
                  >编辑</el-button
                >
                <el-button
                  v-if="scope.row.status === 5"
                  @click="deleteData(scope.row)"
                  type="primary"
                  size="small"
                  link
                  style="color: #007aff; font-size: 14px"
                  >删除</el-button
                >
              </template>
            </el-table-column>
            <template #empty>
              <el-empty :image="imgNone" />
            </template>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="pagination.pageSizes"
            layout="total,sizes, prev, pager, next, jumper"
            :total="table.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
        <el-dialog
          v-model="dialogVisible"
          :title="title"
          destroy-on-close
          :before-close="handleClose"
          :close-on-click-modal="false"
          :width="componentTag === importComponent ? '800px' : '520px'"
        >
          <component
            :is="componentTag"
            @operationDone="operationDone"
            :operationId="operationId"
          >
          </component>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script setup>
import breadcrumb from "@/components/breadcrumb/index.vue";
import Search from "./components/search.vue";
import iconAdd from "@/assets/image/btn/icon-add.png";
import iconImport from "@/assets/image/btn/icon-import.png";
import { reactive, ref, shallowRef } from "vue";
import addComponent from "./components/addComponent.vue";
import editComponent from "./components/editComponent.vue";
import importComponent from "./components/importComponent.vue";
import {
  getBerth,
  getDeleteBerth,
} from "./request.js";

// 搜索
const searchModel = ref({
  code: "",
  regionId: "",
  sectionId: "",
});
const getSearchParams = (value) => {
  // searchParams.value = value;
  searchModel.value = value;
  getData();
};

// 弹窗
const title = ref("");
const dialogVisible = ref(false);
const componentTag = shallowRef("");
const operationId = ref("");
const operationDone = (r) => {
  dialogVisible.value = false;
  operationId.value = "";
  if (!r) {
    getData();
  }
};

// 新增按钮
const addData = () => {
  title.value = "新增泊位信息";
  componentTag.value = addComponent;
  dialogVisible.value = true;
};

// 编辑按钮
const editData = (row) => {
  title.value = "编辑泊位信息";
  operationId.value = row;
  componentTag.value = editComponent;
  dialogVisible.value = true;
};

// 导入
const importData = () => {
  title.value = "导入泊位信息";
  componentTag.value = importComponent;
  dialogVisible.value = true;
};

// 删除按钮
const deleteData = (row) => {
  ElMessageBox.confirm(
    `您确认要删除泊位 [${row.section}${row.code}]?`,
    "温馨提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(async () => {
    await getDeleteBerth(row.id).then((res) => {
      if (res.data.code === 0) {
        getData();
        ElMessage({
          type: "success",
          message: `已删除泊位 [${row.section}${row.code}]`,
        });
      } else {
        ElMessage.error(res.data.msg);
      }
    });
  });
};

// 表格
const table = reactive({
  data: [],
  total: 0,
  loading: true,
});

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  pageSizes: [10, 20, 30, 50],
});

const handleCurrentChange = (value) => {
  pagination.currentPage = value;
  getData();
};

const handleSizeChange = (value) => {
  pagination.pageSize = value;
  getData();
};

const getData = async () => {
  await getBerth({
    code: searchModel.value.code,
    regionId: searchModel.value.regionId,
    sectionId: searchModel.value.sectionId,
    pageNo: pagination.currentPage,
    pageSize: pagination.pageSize,
  }).then((res) => {
    if (res.data.code === 0) {
      table.data = res.data.result.items;
      table.total = parseInt(res.data.result.total);
      table.loading = false;
    }
  });
};
getData();
</script>

<style lang="less" scoped>
.berth-page {
  .search-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px 24px 8px 24px;
    background: #fff;
    border-radius: 6px;
  }
  .table-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px;
    background: #fff;
    border-radius: 6px;
  }
  .table-component {
    .submit-btns {
      display: flex;
      justify-content: space-between;
      margin-bottom: 24px;
    }
    .submit-btns-right {
      display: flex;
    }
    .submit-btn-add {
      background: #007aff;
      border: 1px solid #007aff;
      border-radius: 2px;
      margin-left: 87px;
      padding: 5px 16px;
    }
    .submit-btn-import {
      background: #00d1ff;
      border: 1px solid #00d1ff;
      border-radius: 2px;
      margin-left: 12px;
      padding: 5px 16px;
    }
    .icon-img {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
    .table {
      margin-bottom: 15px;
      .planning {
        width: 52px;
        height: 20px;
        line-height: 18px;
        border: 1px solid #ab00ff;
        border-radius: 2px;
        font-weight: 400;
        font-size: 14px;
        color: #ab00ff;
        text-align: center;
      }
      .NotProduction {
        width: 52px;
        height: 20px;
        line-height: 18px;
        border: 1px solid #007aff;
        border-radius: 2px;
        font-weight: 400;
        font-size: 14px;
        color: #007aff;
        text-align: center;
      }
      .inUse {
        width: 52px;
        height: 20px;
        line-height: 18px;
        border: 1px solid #35c362;
        border-radius: 2px;
        font-weight: 400;
        font-size: 14px;
        color: #35c362;
        text-align: center;
      }

      .underRepair {
        width: 52px;
        height: 20px;
        line-height: 18px;
        border: 1px solid #f18532;
        border-radius: 2px;
        font-weight: 400;
        font-size: 14px;
        color: #f18532;
        text-align: center;
      }
      .scrapped {
        width: 52px;
        height: 20px;
        line-height: 18px;
        border: 1px solid #999999;
        border-radius: 2px;
        font-weight: 400;
        font-size: 14px;
        color: #999999;
        text-align: center;
      }
    }
    .pagination {
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
</style>